package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.TagDao;
import com.clz.hyperit.entity.Position;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.vo.PageInfo;

@Repository("tagDao")
public class TagDaoImpl extends BaseDaoImpl<Tag> implements TagDao {
	public Tag getByName(String name) {
		String hql = "from Tag where name = :name";
		return (Tag) getCurrentSession().createQuery(hql)
				.setString("name", name).uniqueResult();
	}

	public Tag findById(Integer id) {
		String hql = "from Tag where id = :id";
		return (Tag) getCurrentSession().createQuery(hql).setInteger("id", id)
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Tag> findBySearch(PageInfo pageInfo, String tag) {
		String hql = "from Tag where name like :name";
		List<Tag> list = getCurrentSession()
				.createQuery(hql)
				.setString("name", "%" + tag + "%")
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Tag  where name like :name";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setString("name", "%" + tag + "%")
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;

	}

	@SuppressWarnings("unchecked")
	public List<Tag> findByArticleNum(PageInfo pageInfo) {
		String hql = "from Tag order by article desc ";
		List<Tag> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Tag ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Tag> findByQuestionNum(PageInfo pageInfo) {
		String hql = "from Tag order by question desc ";
		List<Tag> list = getCurrentSession()
				.createQuery(hql)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from Tag ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}
}
